﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DiscoveryLogic.UI.Layouts.Controls
{
    public partial class DoubleCylinderBarGraph : UserControl
    {
        #region Private Variables

            double minValue, maxValue;
            double maxTop = 257.16;
            double maxBottom = 364;
            double bar1Value, bar2Value, bar3Value;

        #endregion

        #region Constructors

            public DoubleCylinderBarGraph()
                : this(0,100)
            {
            }

            public DoubleCylinderBarGraph(double minValue, double maxValue)
            {
                // Required to initialize variables
                InitializeComponent();

                this.MaxValue = maxValue;
                this.MinValue = MinValue;
            }
        #endregion

        #region Public Properties

            public double MaxValue
            {
                get { return maxValue; }
                set { maxValue = value; }
            }

            public double MinValue
            {
                get { return minValue; }
                set { minValue = value; }
            }

            public double Bar1Value
            {
                get { return bar1Value; }
                set 
                { 
                    bar1Value = value;
                    AdjustCylinders(1);
                }
            }

            public double Bar2Value
            {
                get { return bar2Value; }
                set 
                { 
                    bar2Value = value;
                    AdjustCylinders(2);
                }
            }

            public double Bar3Value
            {
                get { return bar3Value; }
                set 
                { 
                    bar3Value = value;
                    AdjustCylinders(3);
                }
            }

        #endregion

        #region Helper Methods

            private void AdjustCylinders(int id)
            {
                double percent;
                double length;

                switch(id)
                {
                    case 1:
                        percent = bar1Value / maxValue;
                        length = percent * (maxBottom - maxTop);
                        
                        Bar1Top.SetValue(Canvas.TopProperty, maxBottom - length);
                        Bar1TopReflection.SetValue(Canvas.TopProperty, maxBottom - length);
                        Bar1Body.SetValue(Canvas.TopProperty, maxBottom - length);
                        Bar1Body.Height = length + 53;
                        break;
                    case 2:
                        percent = bar2Value / maxValue;
                        length = percent * (maxBottom - maxTop);

                        Bar2Top.SetValue(Canvas.TopProperty, (double)Bar3Top.GetValue(Canvas.TopProperty) - length);
                        Bar2TopReflection.SetValue(Canvas.TopProperty, (double)Bar3Top.GetValue(Canvas.TopProperty) - length);
                        Bar2Body.SetValue(Canvas.TopProperty, (double)Bar3Top.GetValue(Canvas.TopProperty) - length);
                        Bar2Body.Height = length + 53;
                        
                        break;

                    case 3:
                        percent = bar3Value / maxValue;
                        length = percent * (maxBottom - maxTop);
                        double oldTopPosition = (double)Bar3Top.GetValue(Canvas.TopProperty);
                        double oldBodyPosition = (double)Bar3Body.GetValue(Canvas.TopProperty);
                        Bar3Top.SetValue(Canvas.TopProperty, maxBottom - length);
                        Bar3Body.SetValue(Canvas.TopProperty, maxBottom - length);
                        Bar3Body.Height = length + 53;
                        
                        double topPositionDiff = maxBottom - length - oldTopPosition;
                        double bodyPositionDiff = maxBottom - length - oldBodyPosition;
                        Bar2Top.SetValue(Canvas.TopProperty, (double)Bar2Top.GetValue(Canvas.TopProperty) + topPositionDiff);
                        Bar2TopReflection.SetValue(Canvas.TopProperty, (double)Bar2TopReflection.GetValue(Canvas.TopProperty) + topPositionDiff);
                        Bar2Body.SetValue(Canvas.TopProperty, (double)Bar2Body.GetValue(Canvas.TopProperty) + bodyPositionDiff);
                        Bar2Base.SetValue(Canvas.TopProperty, maxBottom - length-1);
                        break;
                    default:
                        break;
                }  
            }

        #endregion

        #region Public Methods
        #endregion

        #region Event Handlers
        #endregion

        #region Events
        #endregion
    }
}
